package com.four.peoplemanage.information.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.four.peoplemanage.common.bean.ResponseData;
import com.four.peoplemanage.common.bean.ResultCode;
import com.four.peoplemanage.common.controller.BaseController;
import com.four.peoplemanage.information.entity.Information;
import com.four.peoplemanage.information.service.InformationService;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 王瑞
 */
@RestController
@CrossOrigin
@RequestMapping("/information")
public class InformationController extends BaseController {
    @Autowired
    private InformationService informationService;
    //查看
    @GetMapping("/people")
    public ResponseData<Object> getPeople(
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "number",required = false) Integer number,
            @RequestParam(value = "sex",required = false) String sex,
            @RequestParam(value = "employee",required = false) String employee,
            @RequestParam(value = "grade",required = false) String grade,
            @RequestParam(value = "duties",required = false) String duties,
            @RequestParam(value = "education",required = false) String education,
            @RequestParam(value = "marriage",required = false) String marriage
    ){
        return returnObject(informationService.getOnrePage(pageNum,pageSize,name,number, sex, employee, grade, duties, education, marriage));
    }

    @GetMapping("/epeople")
    public ResponseData<Object> getPeople1(
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "number",required = false) Integer number,
            @RequestParam(value = "sex",required = false) String sex,
            @RequestParam(value = "employee",required = false) String employee,
            @RequestParam(value = "grade",required = false) String grade,
            @RequestParam(value = "duties",required = false) String duties,
            @RequestParam(value = "education",required = false) String education,
            @RequestParam(value = "marriage",required = false) String marriage
    ){
        return returnObject(informationService.getOnrePage1(pageNum,pageSize,name,number, sex, employee, grade, duties, education, marriage));
    }

    //添加人员
    @PostMapping("/add")
    public ResponseData<Boolean> add(Information information){
        return returnBoolean(informationService.addPeople(information));
    }

    //修改人员信息
    @PutMapping("/updatepeople")
    public ResponseData<Boolean> update(@RequestBody Information information){
        return returnBoolean(informationService.updatePeople(information));
    }

    //删除员工
    @DeleteMapping("/del/{id}")
    public ResponseData<Boolean> del(@PathVariable("id") Integer id){
        return returnBoolean(informationService.del(id));
    }

    //修改状态
    //锁定商家状态的端点
    @PutMapping("/lock/{id}")
    public ResponseData<Boolean> lockStatus(@PathVariable Integer id) {
        boolean result = informationService.reject(id);
        return new ResponseData<>(1, " status locked successfully", result);
    }

    //解锁商家状态的端点
    @PutMapping("/unlock/{id}")
    public ResponseData<Boolean> unlockStatus(@PathVariable Integer id) {
        boolean result = informationService.approve(id);
        return new ResponseData<>(1, " status unlocked successfully", result);
    }


    //批量导出
    @GetMapping("/export")
    public void exportData(@RequestParam(required = false) String name,
                           HttpServletResponse response) throws IOException {
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //全部导出
        List<Information> list = new ArrayList<>();
        if (StrUtil.isBlank(name) ) {
            list = informationService.selectAll();

        }
        writer.write(list,true);

        response.setContentType("application/ynd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode("用户信息表","UTF-8") + ".xlsx");
        ServletOutputStream outputStream = response.getOutputStream();
        writer.flush(outputStream,true);
        outputStream.flush();
        outputStream.close();
    }

    //批量导入
    @PostMapping("/import")
    public ResultCode importData(@RequestParam("file") MultipartFile file) throws IOException {
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        List<Information> informationList = reader.readAll(Information.class);
        try {
            boolean result = informationService.addAll(informationList);
            return result ? ResultCode.SUCCESS : ResultCode.COMMON_FAIL;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultCode.COMMON_FAIL;
        }
    }


}
